//
//  ImageViewADCell.m
//  CenturyCity
//
//  Created by beisheng03 on 2017/12/18.
//  Copyright © 2017年 beisheng. All rights reserved.
//

#import "ImageViewADCell.h"

@interface ImageViewADCell ()
@property (weak, nonatomic) IBOutlet UIImageView *topImageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomMargin;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bgViewH;

@end

@implementation ImageViewADCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.bottomMargin.constant = 0;
    
    
    self.topImageView.layer.masksToBounds = YES;
    self.topImageView.layer.cornerRadius = 45;
    
//    [self.topImageView sd_setImageWithURL:[NSURL URLWithString:@"http://pic39.nipic.com/20140311/10600816_135850674000_2.jpg"] placeholderImage:nil];
    // Initialization code
}

- (void)setModel:(MarketHomeModel *)model{
    _model = model;
    
    
    UIImage *image0 = [[[SDWebImageManager sharedManager] imageCache] imageFromDiskCacheForKey:_model.tuijianadvs.firstObject.adv_image];
    
    self.topImageView.image = image0;
    
    
    if (image0) {
        
        CGFloat imageViewH = (KScreenW - 40) / image0.size.width * image0.size.height;
        // 重置放大缩小的比例
        self.bgViewH.constant = imageViewH + 20;
        
        
    } else {
        
        MJWeakSelf;
        [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:_model.tuijianadvs.firstObject.adv_image] options:kNilOptions progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            
            if (image) {
                
                CGFloat imageViewH2 = (KScreenW - 40) / image.size.width * image.size.height;
                // 重置放大缩小的比例
                weakSelf.bgViewH.constant = imageViewH2 + 20;
                
                weakSelf.topImageView.image = image;

            }else{
                
                weakSelf.bgViewH.constant = (KScreenW - 40) / 8.0 * 3;

            
            
            }
            
            
        }];
    }
    
    
    
    
    
}


- (IBAction)btnDidTap:(UIButton *)sender {
    
    if ([self.delegate respondsToSelector:@selector(imageTopBtnDidClickWith:)]) {
        [self.delegate imageTopBtnDidClickWith:self.model.tuijianadvs];
    }
 
}




- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
